//
//  DCMineServicesController.m
//  Demo
//
//  Created by zhaolin on 2023/1/5.
//

#import "DCMineServicesController.h"
//#import <MFMailComposeViewControllerDelegate>
#import <Messages/Messages.h>
#import <MessageUI/MessageUI.h>
@interface DCMineServicesController ()<MFMailComposeViewControllerDelegate>
/**
 "data": {
     "email": "string",
     "phone": "string"
   }
 */
@property (nonatomic, strong) NSDictionary *data;
@end

@implementation DCMineServicesController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setTitleName:@"Online Services"];
    [self getDataFromNet];
    // Do any additional setup after loading the view.
}
- (void)configureUI {
    [super configureUI];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.numberOfLines = 0;
    titleLabel.text = @"Please feedback with your name, phone number and app name, thanks!";
    titleLabel.font = DCFont_PF_Regular(12);
    titleLabel.textColor = [UIColor colorWithString:@"#000000"];
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(self.navgitBar.mas_bottom).mas_offset(30);
    }];
    
    UIView *phoneView = [UIView new];
    phoneView.backgroundColor = [UIColor colorWithString:@"#658900"];
    phoneView.layer.cornerRadius = 7.2;
    phoneView.layer.masksToBounds = YES;
    UITapGestureRecognizer *tapPhone = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callPhone)];
    [phoneView addGestureRecognizer:tapPhone];
    [self.view addSubview:phoneView];
    [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28);
        make.height.mas_equalTo(120);
        make.right.mas_equalTo(-28);
        make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(30);
    }];
    
    UIImageView *kefuImage = [[UIImageView alloc] init];
    kefuImage.image = [UIImage imageNamed:@"kefufill"];
    [phoneView addSubview:kefuImage];
    [kefuImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-37);
        make.centerY.mas_equalTo(phoneView);
        make.width.mas_equalTo(47);
        make.height.mas_equalTo(50);
    }];
    
    UILabel *phoneTitle = [UILabel new];
    phoneTitle.text = @"Customer service hotline";
    phoneTitle.font = DCFont_PF_Medium(13);
    phoneTitle.textColor = [UIColor colorWithString:@"#FFFFFF"];
    [phoneView addSubview:phoneTitle];
    [phoneTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(35);
        make.left.mas_equalTo(32);
    }];
    
    UILabel *phoneLabel = [UILabel new];
    phoneLabel.text = @"***********";
    phoneLabel.textColor = [UIColor colorWithString:@"#FFFFFF"];
    phoneLabel.font = DCFont_PF_Medium(16);
    [phoneView addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(32);
        make.bottom.mas_equalTo(-35);
    }];
    
    UIView *emailView = [UIView new];
    [self.view addSubview:emailView];
    emailView.backgroundColor = [UIColor colorWithString:@"#E1FE97"];
    emailView.layer.cornerRadius = 7.2;
    emailView.layer.masksToBounds = YES;
    UITapGestureRecognizer *tapEmail = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callEmail)];
    [emailView addGestureRecognizer:tapEmail];
    [self.view addSubview:emailView];
    [emailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28);
        make.height.mas_equalTo(120);
        make.right.mas_equalTo(-28);
        make.top.mas_equalTo(phoneView.mas_bottom).mas_offset(20);
    }];
    
    UIImageView *emailImage = [UIImageView new];
    emailImage.image = [UIImage imageNamed:@"youxiang-2"];
    [emailView addSubview:emailImage];
    [emailImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-39);
        make.centerY.mas_equalTo(emailView);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(39);
    }];
    
    UILabel *emailTitle = [UILabel new];
    emailTitle.text = @"Customer service hotline";
    emailTitle.font = DCFont_PF_Medium(13);
    emailTitle.textColor = [UIColor colorWithString:@"#1C221B"];
    [emailView addSubview:emailTitle];
    [emailTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(35);
        make.left.mas_equalTo(32);
    }];
    
    UILabel *emailLabel = [UILabel new];
    emailLabel.text = @"*****@91fintek.com";
    emailLabel.font = DCFont_PF_Medium(16);
    emailLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    [emailView addSubview:emailLabel];
    [emailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(32);
        make.bottom.mas_equalTo(-35);
    }];
}

- (void)getDataFromNet {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DCHttpRequest DCCustomerServiceInfoCompleteBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (responseObject.code == 200) {
            self.data = responseObject.data;
        } else {
            [DCDCToast toastText:responseObject.message];
        }
    }];
}

- (void)callPhone{
    /*
     "data": {
     "email": "string",
     "phone": "string"
     }*/
    NSURL *phoneURL = [NSURL URLWithString:[[NSString alloc] initWithFormat:@"tel://%@",[self.data objectForKey:@"phone"]]];
    [[UIApplication sharedApplication] openURL:phoneURL options:@{} completionHandler:nil];
}

-(void)callEmail {
    /*
     "data": {
     "email": "string",
     "phone": "string"
     }*/
    
    if ([MFMailComposeViewController canSendMail]) { // 用户已设置邮件账户
        [self sendEmailAction]; // 调用发送邮件的代码
    }else {
        [DCDCToast toastText:@"no email"];
    }
    
}
- (void)sendEmailAction
{
    // 邮件服务器
    MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
    // 设置邮件代理
    [mailCompose setMailComposeDelegate:self];
    
    // 设置邮件主题
//    [mailCompose setSubject:@"我是邮件主题"];
    
    // 设置收件人
    [mailCompose setToRecipients:@[[self.data objectForKey:@"email"]]];
    
//    // 设置抄送人
//    [mailCompose setCcRecipients:@[@"邮箱号码"]];
//    // 设置密抄送
//    [mailCompose setBccRecipients:@[@"邮箱号码"]];
    
    /**
     *  设置邮件的正文内容
     */
//    NSString *emailContent = @"我是邮件内容";
    NSString *emailContent = @"";
    // 是否为HTML格式
    [mailCompose setMessageBody:emailContent isHTML:NO];
    // 如使用HTML格式，则为以下代码
    //    [mailCompose setMessageBody:@"<html><body><p>Hello</p><p>World！</p></body></html>" isHTML:YES];
    
    /**
     *  添加附件
     */
//    UIImage *image = [UIImage imageNamed:@"image"];
//    NSData *imageData = UIImagePNGRepresentation(image);
//    [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"custom.png"];
//
//    NSString *file = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
//    NSData *pdf = [NSData dataWithContentsOfFile:file];
//    [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"7天精通IOS233333"];
    
    // 弹出邮件发送视图
    [self presentViewController:mailCompose animated:YES completion:nil];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled: // 用户取消编辑
            NSLog(@"Mail send canceled...");
            break;
        case MFMailComposeResultSaved: // 用户保存邮件
            NSLog(@"Mail saved...");
            break;
        case MFMailComposeResultSent: // 用户点击发送
            NSLog(@"Mail sent...");
            break;
        case MFMailComposeResultFailed: // 用户尝试保存或发送邮件失败
            NSLog(@"Mail send errored: %@...", [error localizedDescription]);
            break;
    }
    
    // 关闭邮件发送视图
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
